﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.model;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    class UBSpawnTable
    {
        private static Logger _log = Logger.getLogger( typeof( UBSpawnTable ).FullName );

        private static UBSpawnTable _instance;

        private Dictionary<int, L1UbSpawn> _spawnTable = new Dictionary<int, L1UbSpawn>();

        public static UBSpawnTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new UBSpawnTable();
            }
            return _instance;
        }

        private UBSpawnTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM spawnlist_ub";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    int npc_templateid = SqlUtils.toInt( item["npc_templateid"] );
                    L1Npc npcTemp = NpcTable.getInstance().getTemplate( npc_templateid );
                    if ( npcTemp == null )
                    {
                        continue;
                    }
                    L1UbSpawn spawnDat = new L1UbSpawn();
                    spawnDat.setId( SqlUtils.toInt( item["id"] ) );
                    spawnDat.setUbId( SqlUtils.toInt( item["ub_id"] ) );
                    spawnDat.setPattern( SqlUtils.toInt( item["pattern"] ) );
                    spawnDat.setGroup( SqlUtils.toInt( item["group_id"] ) );
                    spawnDat.setName( npcTemp.get_name() );
                    spawnDat.setNpcTemplateId( SqlUtils.toInt( item["npc_templateid"] ) );
                    spawnDat.setAmount( SqlUtils.toInt( item["count"] ) );
                    spawnDat.setSpawnDelay( SqlUtils.toInt( item["spawn_delay"] ) );
                    spawnDat.setSealCount( SqlUtils.toInt( item["seal_count"] ) );

                    _spawnTable[spawnDat.getId()] = spawnDat;
                }
            }
            _log.config( "UBモンスター配置リスト " + _spawnTable.Count + "件ロード" );
        }

        public L1UbSpawn getSpawn( int spawnId )
        {/*TTOK*/
            L1UbSpawn ret;
            _spawnTable.TryGetValue( spawnId, out ret );
            return ret;
        }

        /// <summary>
        /// 指定されたUBIDに対するパターンの最大数を返す。
        /// </summary>
        /// <param name="ubId">調べるUBID</param>
        /// <returns>パターンの最大数</returns>
        public int getMaxPattern( int ubId )
        {/*TTOK*/
            int n = 0;
            String sqlstr = "SELECT MAX(pattern) FROM spawnlist_ub WHERE ub_id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = ubId;
                object obj = wrapper.ExecScaler( cmd );
                if ( obj != null && !( obj is DBNull ) )
                {
                    n = Convert.ToInt32( obj );
                }
            }
            return n;
        }

        public L1UbPattern getPattern( int ubId, int patternNumer )
        {
            L1UbPattern pattern = new L1UbPattern();
            foreach ( L1UbSpawn spawn in _spawnTable.Values )
            {
                if ( spawn.getUbId() == ubId && spawn.getPattern() == patternNumer )
                {
                    pattern.addSpawn( spawn.getGroup(), spawn );
                }
            }
            pattern.freeze();

            return pattern;
        }
    }
}
